package panel.selfPathList
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class DateChangeBox extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var backImg:Bitmap;
		private var text:TextField;
		
		private var upBtn:Sprite;
		private var downBtn:Sprite;
		
		public var currentDate:int = 1;
		
		private var dateChangeCallBack:Function;
		
		public function DateChangeBox(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
		}
		
		public function initThis(wid:Number,hei:Number):void
		{
			backImg = new _loader.uiManager.dateChangeBtn as Bitmap;
			backImg.width = wid;
			backImg.height = hei;
			
			this.addChild(backImg);
			
			text = new TextField;
			text.wordWrap = false;
			text.multiline = false;
			text.selectable = false;
			
			this.addChild(text);
			repositionText();
			
			upBtn = new Sprite;
			upBtn.graphics.beginFill(0x000000,0);
			upBtn.graphics.drawRect(0,0,wid / 2,hei);
			upBtn.graphics.endFill();
			this.addChild(upBtn);
			
			upBtn.addEventListener(MouseEvent.MOUSE_UP,lessDateHandler);
			
			downBtn = new Sprite;
			downBtn.graphics.beginFill(0x000000,0);
			downBtn.graphics.drawRect(wid / 2,0,wid / 2,hei);
			downBtn.graphics.endFill();
			this.addChild(downBtn);
			
			downBtn.addEventListener(MouseEvent.MOUSE_UP,moreDateHandler);
		}
		
		protected function moreDateHandler(event:MouseEvent):void
		{
			if(currentDate >= 1){
				currentDate ++;
				repositionText();
				
				if(dateChangeCallBack is Function){
					dateChangeCallBack();
				}
			}
		}
		
		protected function lessDateHandler(event:MouseEvent):void
		{
			if(currentDate > 1){
				currentDate --;
				repositionText();
				
				if(dateChangeCallBack is Function){
					dateChangeCallBack();
				}
			}
		}
		
		private function repositionText():void
		{
			TextFieldUtils.setHtmlText(text,HtmlText.formatSizeColor("<b>第" + currentDate + "天</b>",0x000000,22));
			
			text.width = text.textWidth + 5;
			text.height = text.textHeight + 5;
			
			text.x = (backImg.width - text.width) / 2;
			text.y = (backImg.height - text.height) / 2;
		}
		
		public function set changeCallBack(func:Function):void
		{
			dateChangeCallBack = func;
		}
		
		public function changeCount(num:int):void
		{
			currentDate = num;
			
			repositionText();
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				backImg = null;
				
				upBtn.removeEventListener(MouseEvent.MOUSE_UP,lessDateHandler);
				downBtn.removeEventListener(MouseEvent.MOUSE_UP,moreDateHandler);
			}
		}
	}
}